package com.zndroid.bridge;

import android.util.Log;

import com.zndroid.bridge.widget.WebViewX;
import com.zndroid.bridge.framework.core.OnReturnValue;

/**
 * Created by lazy on 2019-09-21
 * @author lzy
 */
@SuppressWarnings("unused")
public class MessageController {
    private final String TAG = "MessageController";

    private boolean isDebug = BuildConfig.DEBUG;

    public final String CALLBACK_METHOD_NAME = "callback";

    private WebViewX webView;
    public MessageController with(WebViewX webView) {
        this.webView = webView;
        return this;
    }

    public void setDebug(boolean debug) {
        isDebug = debug;
    }

    public void sendMessage(String message) {
        sendMessage(message, null);
    }

    public void sendMessage(String message, OnReturnValue<String> callBackValue) {
        if (null != webView && null != message) {
            if (isDebug) {
                Log.i(TAG, message);
            }

            webView.callHandler(CALLBACK_METHOD_NAME, new String[]{message}, callBackValue);
        }
    }

    //////////////////////// get instance ////////////////////////
    private MessageController() {}

    private static class $ {
        private static MessageController $$ = new MessageController();
    }

    public static MessageController get() {
        return $.$$;
    }
    //////////////////////// get instance ////////////////////////
}
